package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.NewsPlateDao;
import com.cn.wanxi.model.PlateModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsPlateImpl implements NewsPlateDao {
    @Override
    public List<PlateModel> findAll() {
        String sql = "select * from plate";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<PlateModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {

                PlateModel model = new PlateModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setStatus(resultSet.getInt("status"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setRemark(resultSet.getString("remark"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return list;
    }

    @Override
    public int add(PlateModel plateModel) {
        String sql = "insert into plate(name,status,create_time) " +
                "values('" + plateModel.getName() + "'," +
                "'" + plateModel.getStatus() + "','" + plateModel.getCreateTime() + "')";
        return JDBC.excuteUpdate(sql);
    }
}
